import 'dart:io';
import 'dart:ui';

import 'package:farmer_app/widgets/pages/login/login_code_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {

    final TextEditingController _phonePreController = TextEditingController(text: '+86');
    final TextEditingController _phoneController = TextEditingController();
    String phoneLabel = '请输入手机号';

    return Scaffold(
      appBar: AppBar(),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
                padding: const EdgeInsets.only(left: 10, bottom: 50, top: 20),
                child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Image.asset('assets/images/farmer-logo.png', filterQuality: FilterQuality.low, height: 60, width: 60,),
                      const SizedBox(width: 10),
                      const Text('欢迎登录智慧农业', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
                    ]
                )
            ),
            // FittedBox(
            //     child: const Padding(
            //       padding: EdgeInsets.only(left: 0,top: 50),
            //       child: Row(
            //         mainAxisSize: MainAxisSize.min,
            //         children: [
            //           Text('输入手机号码',
            //             style: TextStyle(
            //                 fontSize: 18, color:Colors.grey,
            //             ),
            //           )
            //         ],
            //       ),
            //     )
            // ),
            Padding(
              padding: EdgeInsets.only(left: 0, top: 50, bottom: 50),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // Text('+86',
                  //   style: TextStyle(
                  //     fontSize: 22,
                  //     // decoration: TextDecoration.underline, // 添加下划线
                  //   ),
                  // ),
                  Container(
                      width: 40,
                      child: TextField(
                        controller: _phonePreController,
                        decoration: InputDecoration(
                          labelText: '中国', // 提示文字
                        ),
                        readOnly: true,
                      )),
                  SizedBox(
                    width: 20,
                  ),
                  Container(
                      width: 200,
                      child: TextField(
                        controller: _phoneController,
                        decoration: InputDecoration(
                          prefixIcon: Icon(Icons.phone), // 图标
                          labelText: phoneLabel, // 提示文字
                        ),
                        keyboardType: TextInputType.phone, // 键盘类型
                        onChanged: (value) {
                          // 输入变化时的回调函数
                          // 可以在这里进行校验等操作
                          if (value.length == 11) {
                            // 校验通过后，可以调用setState方法刷新界面
                          }
                        },
                      ))
                ],
              ),
            ),
            Padding (
              padding: EdgeInsets.only(left: 0, top: 80,bottom: 50),
              child: ElevatedButton(
                  onPressed: (){
                    String phone = _phoneController.text;
                    print(phone);
                    // if(validPhone(phone)){
                    //
                    Navigator.push(context, MaterialPageRoute(builder: (context) => LoginCodePage(phoneNumber: phone)));
                    // } else {
                    //   // todo 加错误提示
                    // }
                  },
                  style: ElevatedButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: Colors.blue,
                    minimumSize: Size(360, 60),
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                  ),
                  child: const Text('授权登录')
              ),
            ),
            // Padding (
            //   padding: EdgeInsets.only(left: 0, top: 20),
            //   child: ElevatedButton(
            //       onPressed: (){},
            //       style: ElevatedButton.styleFrom(
            //         shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            //         minimumSize: Size(360, 60),
            //       ),
            //       child: const Text('切换手机号')
            //   ),
            // ),
            Padding (
              padding: EdgeInsets.only(left: 0, top: 100),
              child: RichText(
                  textAlign: TextAlign.center,
                  text: const TextSpan(
                      text: 'Read our',
                      style: TextStyle(
                          color: Colors.black,
                          height: 1.5
                      ),
                      children: [
                        TextSpan(
                          text: 'Privacy Policy',
                          style: TextStyle(
                            color: Colors.blue,
                          ),
                        ),
                        TextSpan(
                          text: 'Tap "Aggree and continue" to accept the. ',
                        ),
                        TextSpan(
                          text: 'Terms of Services.',
                          style: TextStyle(
                            color: Colors.blue,
                          ),
                        ),
                      ]
                  )
              ),
            ),
          ],
        ),
      )
    );
  }
  // 校验手机号格式
  bool validPhone(String phone) {
    // 使用正则表达式校验手机号的格式
    RegExp regex = RegExp(r'^1[0-9]{10}$');
    return regex.hasMatch(phone);
  }
}
